package com.ifacebox.face.track.utils;

import com.google.gson.Gson;
import com.ifacebox.face.track.model.FaceUser;

import java.nio.charset.StandardCharsets;

/**
 * @author znn
 */
public class JsonUtils {
    private static final Gson GSON = new Gson();

    public static <T> T fromJson(byte[] json, Class<T> clazz) {
        return fromJson(new String(json, StandardCharsets.UTF_8), clazz);
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        try {
            return GSON.fromJson(json, clazz);
        } catch (RuntimeException e) {
            throw new RuntimeException(String.format("Json解析异常！(%s)", e.getMessage()));
        }
    }

    public static String toJson(Object obj) {
        try {
            return GSON.toJson(obj);
        } catch (RuntimeException e) {
            throw new RuntimeException(String.format("Json解析异常！(%s)", e.getMessage()));
        }
    }
}
